package com.ruoyi.web.controller.airplane;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.system.domain.dto.ItemDTO;
import com.ruoyi.system.domain.dto.TaskCreateDTO;
import com.ruoyi.system.domain.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbTask;
import com.ruoyi.system.service.ITbTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 推送任务Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags = "推送任务管理")
@RestController
@RequestMapping("/system/task")
public class TbTaskController extends BaseController {
    @Autowired
    private ITbTaskService tbTaskService;

    /**
     * 查询推送任务列表
     */
    @ApiOperation("查询推送任务列表")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) String taskName,
                              @RequestParam(required = false) Integer taskStatus,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date registrationTimeBegin,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date registrationTimeEnd) {


        return   tbTaskService.selectTbTaskVOLis(taskName,taskStatus,registrationTimeBegin,registrationTimeEnd);

    }


    /**
     * 导出推送任务列表
     */
    @ApiOperation("导出推送任务列表")
    //@PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "推送任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTask tbTask) {
        List<TbTask> list = tbTaskService.selectTbTaskList(tbTask);
        ExcelUtil<TbTask> util = new ExcelUtil<TbTask>(TbTask.class);
        util.exportExcel(response, list, "推送任务数据");
    }

    /**
     * 获取推送任务详细信息
     */
    @ApiOperation("获取推送任务详细信息")
    //  @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbTaskService.selectTbTaskById(id));
    }


    /**
     * 修改推送任务
     */
    @ApiOperation("修改推送任务")
    // @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "推送任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTask tbTask) {
        return toAjax(tbTaskService.updateTbTask(tbTask));
    }

    /**
     * 删除推送任务
     */
    @ApiOperation("删除推送任务")
    // @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "推送任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbTaskService.deleteTbTaskByIds(ids));
    }


    @ApiOperation("创建任务")
    @PostMapping("/create/task")
    public R createTask(@Valid @RequestBody TaskCreateDTO dto) {
        return R.ok(tbTaskService.CreateTaskAnddetailsToDevice(dto));
    }


    @ApiOperation("全量发布/飞机/设备信息/统计")
    @PostMapping("/publish/all/device/info")
    public R<AviationAppStatsVO> publishAllDeviceInfo(@RequestBody List<ItemDTO> itemDTOS) {
        return R.ok(tbTaskService.publishAllDeviceInfo(itemDTOS));
    }


    @ApiOperation("按飞机号/飞机/设备信息/统计")
    @GetMapping("/publish/byPlaneCode/device/info")
    public R<List<AirPlaneAndDeviceInfoVO>> publishByPlaneCodeDeviceInfo() {
        return R.ok(tbTaskService.publishByPlaneCodeDeviceInfo());
    }


    @ApiOperation("条件查询/设备信息/统计")
    @GetMapping("/publish/byDeviceSn/device/info")
    public R<List<AirPlaneAndDeviceDetailInfoVO>> publishByDeviceSnDeviceInfo(@RequestParam(required = false) String deviceSn,
                                                                              @RequestParam(required = false) String planeCode) {
        return R.ok(tbTaskService.publishByDeviceSnDeviceInfo(deviceSn, planeCode));
    }

}
